package frame;


import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.JTextField;

import bean.BusType;
import dao.BusStopDaoImpl;

import javax.swing.JCheckBox;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;

public class BusInformation implements ItemListener,ActionListener{

    private JFrame frame;
    private JTextField textField;
    private JTextField textField_1;

    JCheckBox checkBox_3;
    JCheckBox checkBox_3_1;
    JCheckBox checkBox_3_2;
    JCheckBox checkBox_3_3;
    JCheckBox checkBox_3_4;
    JCheckBox checkBox_3_5;
    JCheckBox checkBox_3_6;
    JCheckBox checkBox_3_7;
    JComboBox comboBox;

    JButton button;

    String btype="";
    String busstop="";

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    BusInformation window = new BusInformation();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public BusInformation() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 590, 500);
        frame.setLocationRelativeTo(null); // Ĭ Ͼ     ʾ
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        frame.setVisible(true);

        JPanel panel = new JPanel();
        panel.setBounds(0, 0, 572, 453);
        frame.getContentPane().add(panel);
        panel.setLayout(null);

        JLabel label = new JLabel("\u516C\u4EA4\u8F66\u4FE1\u606F\u5F55\u5165");
        label.setBounds(181, 13, 210, 35);
        label.setFont(new Font("    ", Font.PLAIN, 30));
        panel.add(label);

        JLabel label_1 = new JLabel("\u53F8\u673A\u7F16\u53F7\uFF1A");
        label_1.setFont(new Font("    ", Font.PLAIN, 20));
        label_1.setBounds(45, 86, 117, 31);
        panel.add(label_1);

        textField = new JTextField();
        textField.setColumns(10);
        textField.setBounds(181, 91, 86, 24);
        panel.add(textField);

        JLabel label_1_1 = new JLabel("\u8F66\u724C\u53F7\uFF1A");
        label_1_1.setFont(new Font("    ", Font.PLAIN, 20));
        label_1_1.setBounds(45, 145, 117, 31);
        panel.add(label_1_1);

        JLabel label_1_2 = new JLabel("\u516C\u4EA4\u8F66\u7C7B\u578B\uFF1A");
        label_1_2.setFont(new Font("    ", Font.PLAIN, 20));
        label_1_2.setBounds(45, 201, 128, 31);
        panel.add(label_1_2);

        JLabel label_1_3 = new JLabel("\u516C\u4EA4\u8F66\u7AD9\uFF1A");
        label_1_3.setFont(new Font("    ", Font.PLAIN, 20));
        label_1_3.setBounds(45, 263, 117, 31);
        panel.add(label_1_3);

        textField_1 = new JTextField();
        textField_1.setColumns(10);
        textField_1.setBounds(181, 150, 86, 24);
        panel.add(textField_1);

        checkBox_3 = new JCheckBox("\u9C7C\u5C71\u8DEF");
        checkBox_3.setBounds(167, 267, 77, 27);
        panel.add(checkBox_3);

        checkBox_3_1 = new JCheckBox("\u5C71\u4E1C\u8DEF");
        checkBox_3_1.setBounds(244, 267, 77, 27);
        panel.add(checkBox_3_1);

        checkBox_3_2 = new JCheckBox("\u4E5D\u6C5F\u8DEF");
        checkBox_3_2.setBounds(327, 267, 77, 27);
        panel.add(checkBox_3_2);

        checkBox_3_3 = new JCheckBox("\u71D5\u513F\u5C9B\u8DEF");
        checkBox_3_3.setBounds(28, 303, 100, 27);
        panel.add(checkBox_3_3);

        checkBox_3_4 = new JCheckBox("\u5927\u5B66\u8DEF");
        checkBox_3_4.setBounds(128, 303, 77, 27);
        panel.add(checkBox_3_4);

        checkBox_3_5 = new JCheckBox("\u4EBA\u6C11\u8DEF");
        checkBox_3_5.setBounds(211, 303, 77, 27);
        panel.add(checkBox_3_5);

        checkBox_3_6 = new JCheckBox("\u5EB7\u5B81\u8DEF");
        checkBox_3_6.setBounds(294, 303, 77, 27);
        panel.add(checkBox_3_6);

        checkBox_3_7 = new JCheckBox("\u5468\u53E3\u8DEF");
        checkBox_3_7.setBounds(387, 303, 77, 27);
        panel.add(checkBox_3_7);

        button = new JButton("\u63D0\u4EA4\u4FE1\u606F");
        button.setBounds(229, 382, 113, 27);
        panel.add(button);

        comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"\u666E\u901A\u516C\u4EA4", "\u7535\u8F66", "\u53CC\u5C42\u5DF4\u58EB"}));
        comboBox.setBounds(181, 206, 86, 24);
        panel.add(comboBox);

        button.addActionListener(this);
        comboBox.addItemListener(this);
        checkBox_3.addItemListener(this);
        checkBox_3_1.addItemListener(this);
        checkBox_3_2.addItemListener(this);
        checkBox_3_3.addItemListener(this);
        checkBox_3_4.addItemListener(this);
        checkBox_3_5.addItemListener(this);
        checkBox_3_6.addItemListener(this);
        checkBox_3_7.addItemListener(this);
    }

    @Override
    public void itemStateChanged(ItemEvent e) {
        // TODO Auto-generated method stub
        busstop="";
        if(checkBox_3.isSelected()){
            busstop+=checkBox_3.getText()+"-";
        }
        if(checkBox_3_1.isSelected()){
            busstop+=checkBox_3_1.getText()+"-";
        }
        if(checkBox_3_2.isSelected()){
            busstop+=checkBox_3_2.getText()+"-";
        }
        if(checkBox_3_3.isSelected()){
            busstop+=checkBox_3_3.getText()+"-";
        }
        if(checkBox_3_4.isSelected()){
            busstop+=checkBox_3_4.getText()+"-";
        }
        if(checkBox_3_5.isSelected()){
            busstop+=checkBox_3_5.getText()+"-";
        }
        if(checkBox_3_6.isSelected()){
            busstop+=checkBox_3_6.getText()+"-";
        }
        if(checkBox_3_7.isSelected()){
            busstop+=checkBox_3_7.getText()+"-";
        }
        btype=comboBox.getSelectedItem().toString();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        int did=Integer.parseInt(textField.getText());
        String bnum=textField_1.getText();
        BusStopDaoImpl bsdi=new BusStopDaoImpl();
        BusType bus=new BusType();
        bus.setDid(did);
        bus.setBnum(bnum);
        bus.setBtype(btype);
        bus.setBusstop(busstop);
        int i=bsdi.addBus(bus);
        if(i!=0){
            JOptionPane.showMessageDialog(frame, "ע  ɹ   ");
            frame.setVisible(false);
        }
    }
}